package org.jeecg.modules.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.company.entity.WlhyCompanyDriver;
import org.jeecg.modules.company.mapper.WlhyCompanyDriverMapper;
import org.jeecg.modules.company.service.IWlhyCompanyDriverService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS0;
import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS1;

/**
 * @Description: 公司常用司机表
 * @Author: jeecg-boot
 * @Date:   2024-04-16
 * @Version: V1.0
 */
@Service
public class WlhyCompanyDriverServiceImpl extends ServiceImpl<WlhyCompanyDriverMapper, WlhyCompanyDriver> implements IWlhyCompanyDriverService {

    @Override
    public void add(WlhyCompanyDriver wlhyCompanyDriver) {
        WlhyCompanyDriver companyDriver = getOne(new LambdaQueryWrapper<WlhyCompanyDriver>()
                .eq(WlhyCompanyDriver::getCompanyId, wlhyCompanyDriver.getCompanyId())
                .eq(WlhyCompanyDriver::getDriverVehicleNo, wlhyCompanyDriver.getDriverVehicleNo())
                .eq(WlhyCompanyDriver::getDriverBankCardNo, wlhyCompanyDriver.getDriverBankCardNo())
        );
        if (null == companyDriver) {
            wlhyCompanyDriver.setStatus(ROW_STATUS1);
            save(wlhyCompanyDriver);
        }else{
            String companyDriverId = companyDriver.getId();
            BeanUtils.copyProperties(wlhyCompanyDriver,companyDriver);
            companyDriver.setId(companyDriverId);
            companyDriver.setStatus(ROW_STATUS1);
            updateById(companyDriver);
        }
    }

    @Override
    public void updateEnable(String id, String status) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        update(new LambdaUpdateWrapper<WlhyCompanyDriver>()
                .eq(WlhyCompanyDriver::getWaybillPubUserId,loginUser.getId())
                .eq(WlhyCompanyDriver::getId,id)
                .set(WlhyCompanyDriver::getStatus,status));
    }

    @Override
    public void delete(String id) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        update(new LambdaUpdateWrapper<WlhyCompanyDriver>()
                .eq(WlhyCompanyDriver::getWaybillPubUserId,loginUser.getId())

                .eq(WlhyCompanyDriver::getId,id).set(WlhyCompanyDriver::getStatus,ROW_STATUS0));
    }

    @Override
    public void deleteBatchByIds(List<String> ids) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        update(new LambdaUpdateWrapper<WlhyCompanyDriver>()
                .in(WlhyCompanyDriver::getId,ids)
                .eq(WlhyCompanyDriver::getWaybillPubUserId,loginUser.getId())

                .set(WlhyCompanyDriver::getStatus,ROW_STATUS0));

    }
}
